﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using APILocawebGateway.VISA;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("TestProject")]
namespace APILocawebGateway.VISA
{
    internal class TID
    {
        public static String GerarTid(ConfiguracaoVISA configuracaoVisaVBV, Int32 quantidadeParcelas)
        {
            String quantidadeParcelasAsString = quantidadeParcelas.ToString();
            for (int i = quantidadeParcelasAsString.Length; i < 3; i++)
                quantidadeParcelasAsString = "0" + quantidadeParcelasAsString;

            return FormataAfiliacaoVisa(configuracaoVisaVBV.Afiliacao) + RecuperaUltimoDigitoDoAno(DateTime.Now) + RecuperaDataJuliana(DateTime.Now) + RecuperaHorarioFormatoVisa(DateTime.Now) + (Int32)configuracaoVisaVBV.FormaPagamento + quantidadeParcelasAsString;

        }

        #region DateFormat

        public static String RecuperaDataJuliana(DateTime date)
        {
            DateTime sinceDate = new DateTime(DateTime.Now.Year, 1, 1);
            String julianDateAsString = (date.Date.AddDays(1) - sinceDate).Days.ToString();
            for (int i = julianDateAsString.Length; i < 3; i++)
                julianDateAsString = "0" + julianDateAsString; ;

            return julianDateAsString;

        }

        public static String RecuperaHorarioFormatoVisa(DateTime dateTime)
        {
            String hour = dateTime.Hour.ToString();
            String minutes = dateTime.Minute.ToString();
            String seconds = dateTime.Second.ToString();
            if (hour.Length == 1)
                hour = "0" + hour;

            if (minutes.Length == 1)
                minutes = "0" + minutes;

            if (seconds.Length == 1)
                seconds = "0" + seconds;


            return (hour + minutes + seconds + (dateTime.Millisecond / 100));
        }

        public static String FormataAfiliacaoVisa(String afiliacao)
        {
            return afiliacao.Substring(4, 5);
        }

        public static String RecuperaUltimoDigitoDoAno(DateTime date)
        {
            return date.Year.ToString().Substring(3);
        }

        public static DateTime DaysSinceThisYear(int days)
        {
            return new DateTime(DateTime.Now.Year, 1, 1).AddDays(days);
        }


        #endregion
    }
}
